class changMsg{
    constructor(){
       this.getMsg();
    }
    //获取个人信息
    async getMsg(){
       let userId = localStorage.getItem('user_id');
    //    console.log(userId);
       //发送ajax请求获取用户信息
       axios.defaults.headers['authorization'] = 'application/x-www-form-urlencoded';
     let { status,data } = await axios.get('http://localhost:8888/users/info?id='+userId);
    //  console.log(res);
      //   console.log(data.info);
         if(status!=200||data.code!=1) return
         //将用户信息渲染到页面中
         let html = '';
         var arr = [];
         let info = data.info;
         // console.log(info);
         for(let i in info){
             arr.push({[i]:info[i]});
         }
         // console.log(arr);
         localStorage.setItem('nickname',arr[2].nickname);
         arr.forEach(()=>{
            html = `<div class="grzlbt ml40">我的资料</div>
            <div class="subgrzl ml40"><span>昵称</span><span>${arr[2].nickname}</span><span><a href="#none" onclick='changMsg.changeName(${arr[0].id})'>编辑</a></span></div>
            <div class="subgrzl ml40"><span>手机号</span><span>15669097417</span><span><a href="">编辑</a></span></div>
            <div class="subgrzl ml40"><span>密码</span><span>***********</span><span><a href="">编辑</a></span></div>
            <div class="subgrzl ml40"><span>个性签名</span><span>披金成王,伴坤远航</span><span><a href="">编辑</a></span></div>
            <div class="subgrzl ml40"><span>我的爱好</span><span>唱,跳,rap,篮球</span><span><a href="">编辑</a></span></div>
            <div class="subgrzl ml40"><span>收货地址</span><span>千峰教育</span><span><a href="">编辑</a></span></div>
      `
         })
         this.$('.rtcont').innerHTML = html;
      
    }
    static changeName(userId){
      // console.log(1111);
      //发送ajax请求
      // let nickname = localStorage.getItem('nickname')
      // console.log(nickname);
      
         layer.prompt({title: '请输入你要修改的昵称', formType: 2}, function(text, index){
           layer.close(index);
           let nickname = text ;
           let param = `id=${userId}&nickname=${nickname}`
       axios.post('http://localhost:8888/users/update',param).then(res=>{
         // console.log(res);
       })
       window.location.reload();
         });
      
    }
    $(ele){
      let res = document.querySelectorAll(ele);
      return res.length==1 ? res[0] : res;
  }
}
new changMsg;